package com.lujianfei.powersearch.ext

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.AnimatorListenerAdapter
import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableArrayList
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.lujianfei.powersearch.adapter.CommonAdapter
import com.lujianfei.powersearch.adapter.SearchAdapter
import com.lujianfei.powersearch.database.SearchItemModel
import com.lujianfei.powersearch.logger.LoggerFactory
import com.lujianfei.powersearch.ui.createOrUpdate.widget.imageviewedit.model.ImageViewEditItem
import com.lujianfei.powersearch.ui.report.adapter.ReportAdapter
import com.lujianfei.powersearch.ui.report.model.ReportItemModel
import java.io.File


@BindingAdapter(value = ["bindClick"], requireAll = false)
fun bindClick(
    view: View,
    bindClick: View.OnClickListener? = null
) {
    view.setOnClickListener {
        bindClick?.onClick(it)
    }
}

@BindingAdapter(value = ["bindLocalImage", "bindLocalImagePlaceHolder"], requireAll = false)
fun bindLocalImage(
    imageView: ImageView,
    path: String,
    placeholde: Drawable?
) {
    if (path.isNotBlank()) {
        Glide.with(imageView).load(File(path)).placeholder(placeholde).into(imageView)
    }
}

@BindingAdapter(value = ["bindLocalImageUri", "bindLocalImageUriPlaceHolder"], requireAll = false)
fun bindLocalImageUri(
    imageView: ImageView,
    path: Uri?,
    placeholder: Drawable?
) {
    if (path != null) {
        Glide.with(imageView).load(path).placeholder(placeholder).into(imageView)
    } else {
        imageView.setImageDrawable(placeholder)
    }
}

@BindingAdapter(value = ["bindImageViewEditItem", "bindLocalImageUriPlaceHolder"], requireAll = false)
fun bindImageViewEditItem(
    imageView: ImageView,
    path: ImageViewEditItem?,
    placeholder: Drawable?
) {
    LoggerFactory.getLogger().d("bindImageViewEditItem","$path")
    if (path != null) {
        if (path.hasUri()) {
            Glide.with(imageView).load(path.imageUri).placeholder(placeholder).into(imageView)
        } else if (path.hasUrl()) {
            Glide.with(imageView).load(path.imageUrl).placeholder(placeholder).into(imageView)
        } else {
            imageView.setImageDrawable(placeholder)
        }
    } else {
        imageView.setImageDrawable(placeholder)
    }
}

@BindingAdapter(
    value = ["bindCommonAdapter", "onItemClick", "onItemLongClick"],
    requireAll = false
)
fun bindCommonAdapter(
    recyclerView: RecyclerView,
    data: ObservableArrayList<SearchItemModel>?,
    onItemClick: ((SearchItemModel) -> Unit)? = null,
    onItemLongClick: ((SearchItemModel) -> Unit)? = null,
) {
    if (recyclerView.adapter != null || data == null) {
        return
    }
    val commonAdapter = CommonAdapter(data)
    commonAdapter.onItemClick = onItemClick
    commonAdapter.onItemLongClick = onItemLongClick
    recyclerView.apply {
        layoutManager = LinearLayoutManager(this.context)
        addItemDecoration(DividerItemDecoration(this.context, DividerItemDecoration.VERTICAL))
        adapter = commonAdapter
    }
}


@BindingAdapter(
    value = ["bindSearchAdapter", "onItemClick", "onItemLongClick"],
    requireAll = false
)
fun bindSearchAdapter(
    recyclerView: RecyclerView,
    data: ObservableArrayList<SearchItemModel>,
    onItemClick: ((SearchItemModel) -> Unit)? = null,
    onItemLongClick: ((SearchItemModel) -> Unit)? = null,
) {
    if (recyclerView.adapter != null) {
        return
    }
    val commonAdapter = SearchAdapter(data)
    commonAdapter.onItemClick = onItemClick
    commonAdapter.onItemLongClick = onItemLongClick
    recyclerView.apply {
        layoutManager = LinearLayoutManager(this.context)
        addItemDecoration(DividerItemDecoration(this.context, DividerItemDecoration.VERTICAL))
        adapter = commonAdapter
    }
}


@BindingAdapter(value = ["bindReportAdapter"], requireAll = false)
fun bindReportAdapter(
    recyclerView: RecyclerView,
    data: ObservableArrayList<ReportItemModel>,
) {
    if (recyclerView.adapter != null) {
        return
    }
    val reportAdapter = ReportAdapter(data)
    recyclerView.apply {
        layoutManager = LinearLayoutManager(this.context)
        adapter = reportAdapter
    }
}

@BindingAdapter(value = ["bindAnimVisible", "startY", "endY"], requireAll = false)
fun bindAnimVisible(
    view: View,
    visible: Boolean,
    startY:Float,
    endY:Float
) {
    val duration = 300L
    if (view.tag == null) {
        view.tag = true
        view.visibility = if (visible) View.VISIBLE else View.GONE
    } else {
        view.animate().cancel()
        if (visible) {
            view.visibility = View.VISIBLE
            view.translationY = startY
            view.animate().translationY(endY).setDuration(duration).setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    view.translationY = endY
                }
            })
        } else {
            view.translationY = endY
            view.animate().translationY(startY).setDuration(duration).setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    view.translationY = startY
                    view.visibility = View.GONE
                }
            })
        }
    }
}